//
// Created by wya on 2021/5/14.
//

#ifndef NFP_IMAGEPROCESS_H
#define NFP_IMAGEPROCESS_H
#include <iostream>
#include <armadillo>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

/* Extend division reminder to vectors
*
* @param   a       Dividend
* @param   n       Divisor
*/
template<typename T>
T mod(T a, double n)
{
    return a-arma::floor(a/n)*n;
}

static void cvMat2armaMat(const cv::Mat& cv_mat_in, double * arma_mat_out)
{//convert unsigned char cv::Mat to arma::Mat<uchar>

    for (int r = 0; r < cv_mat_in.rows; r++) {
        for (int c = 0; c < cv_mat_in.cols; c++) {
            *(arma_mat_out+c*cv_mat_in.rows+r) = cv_mat_in.data[r*cv_mat_in.cols + c] ;// / 255.0  .data[r*cv_mat_in.cols + c] .at<uchar>(r,c)
        }
    }
};

static void cvMat2armaMat_d(const cv::Mat_<double>& cv_mat_in, double * arma_mat_out)
{//convert unsigned char cv::Mat to arma::Mat<uchar>

    for (int r = 0; r < cv_mat_in.rows; r++) {
        for (int c = 0; c < cv_mat_in.cols; c++) {
            *(arma_mat_out+c*cv_mat_in.rows+r) = cv_mat_in(r,c);// / 255.0
        }
    }
};

template<typename T>

static void armaMat2cvMat(const arma::Mat<T>& arma_mat_in, cv::Mat_<T>& cv_mat_out)
{
    cv::transpose(cv::Mat_<T>(static_cast<int>(arma_mat_in.n_cols),
                              static_cast<unsigned char>(arma_mat_in.n_rows),
                              const_cast<T*>(arma_mat_in.memptr())),
                  cv_mat_out);
};


using namespace arma;

namespace imgproc
{
    void MaskPosition(mat& mask,vec position);
    void Mask_img(mat& src,vec& MaskPosition,mat dst);

}


#endif //NFP_IMAGEPROCESS_H
